package com.ctg.itrdc.sysmgr.permission.core.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.CollectionUtils;

import com.ctg.itrdc.sysmgr.permission.core.dto.CommonRegionDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.FuncCompDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.FuncMenuDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.OrganizationDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.PoliticalLocationDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.StaffDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.SystemRolesDTO;
import com.ctg.itrdc.sysmgr.permission.core.dto.SystemUserDTO;

public class PermissionDaoImpl extends JdbcDaoSupport implements IPermissionDao {

	private String sqlGetRoleCodes;
	private String sqlGetUserByName;
	private String sqlUpdateLoginNum;
	private String sqlCountPostByUser;
	private String sqlSelectPostByUser;
	private String sqlSelectMenu;
	private String sqlSelectComp;
	private String sqlGetRoleIdsByUId;
	private String sqlGetStaffNameById;

	private String getOrgById;
	private String getStaffById;
	private String getPostById;
	private String getUserById;
	private String updatePassword;
	private String sqlGetParentRegion;
	private String sqlGetRzOrgByStaffId;
	private String sqlGetRzOrgCode;
	private String sqlGetOrgByUserIdAndPostId;
	private String sqlGetRegionNameById;
	private String sqlGetPoliticalLocation;
	private String sqlGetPoliticalLocationByRegionId;

	public String getSqlGetRzOrgCode() {
		return sqlGetRzOrgCode;
	}

	public void setSqlGetRzOrgCode(String sqlGetRzOrgCode) {
		this.sqlGetRzOrgCode = sqlGetRzOrgCode;
	}

	public String getSqlGetRoleCodes() {
		return sqlGetRoleCodes;
	}

	public void setSqlGetRoleCodes(String sqlGetRoleCodes) {
		this.sqlGetRoleCodes = sqlGetRoleCodes;
	}

	public String getSqlGetUserByName() {
		return sqlGetUserByName;
	}

	public void setSqlGetUserByName(String sqlGetUserByName) {
		this.sqlGetUserByName = sqlGetUserByName;
	}

	public String getSqlUpdateLoginNum() {
		return sqlUpdateLoginNum;
	}

	public void setSqlUpdateLoginNum(String sqlUpdateLoginNum) {
		this.sqlUpdateLoginNum = sqlUpdateLoginNum;
	}

	public String getSqlCountPostByUser() {
		return sqlCountPostByUser;
	}

	public void setSqlCountPostByUser(String sqlCountPostByUser) {
		this.sqlCountPostByUser = sqlCountPostByUser;
	}

	public String getSqlSelectPostByUser() {
		return sqlSelectPostByUser;
	}

	public void setSqlSelectPostByUser(String sqlSelectPostByUser) {
		this.sqlSelectPostByUser = sqlSelectPostByUser;
	}

	public String getSqlSelectMenu() {
		return sqlSelectMenu;
	}

	public void setSqlSelectMenu(String sqlSelectMenu) {
		this.sqlSelectMenu = sqlSelectMenu;
	}

	public String getSqlSelectComp() {
		return sqlSelectComp;
	}

	public void setSqlSelectComp(String sqlSelectComp) {
		this.sqlSelectComp = sqlSelectComp;
	}

	public void setSqlGetRoleIdsByUId(String sqlGetRoleIdsByUId) {
		this.sqlGetRoleIdsByUId = sqlGetRoleIdsByUId;
	}

	public void setGetOrgById(String getOrgById) {
		this.getOrgById = getOrgById;
	}

	public void setGetStaffById(String getStaffById) {
		this.getStaffById = getStaffById;
	}

	public void setGetPostById(String getPostById) {
		this.getPostById = getPostById;
	}

	public void setGetUserById(String getUserById) {
		this.getUserById = getUserById;
	}

	public void setUpdatePassword(String updatePassword) {
		this.updatePassword = updatePassword;
	}

	public void setSqlGetParentRegion(String sqlGetParentRegion) {
		this.sqlGetParentRegion = sqlGetParentRegion;
	}

	public void setSqlGetRzOrgByStaffId(String sqlGetRzOrgByStaffId) {
		this.sqlGetRzOrgByStaffId = sqlGetRzOrgByStaffId;
	}

	@Override
	public List<String> getRoleCodes(Long sysUserId, Long sysPostId) {
		return getJdbcTemplate().queryForList(sqlGetRoleCodes, String.class,
				sysUserId, sysPostId);
	}

	public void setSqlGetStaffNameById(String sqlGetStaffNameById) {
		this.sqlGetStaffNameById = sqlGetStaffNameById;
	}

	public void setSqlGetOrgByUserIdAndPostId(String sqlGetOrgByUserIdAndPostId) {
		this.sqlGetOrgByUserIdAndPostId = sqlGetOrgByUserIdAndPostId;
	}

	public void setSqlGetRegionNameById(String sqlGetRegionNameById) {
		this.sqlGetRegionNameById = sqlGetRegionNameById;
	}
	
	public void setSqlGetPoliticalLocationByRegionId(String sqlGetPoliticalLocationByRegionId) {
		this.sqlGetPoliticalLocationByRegionId = sqlGetPoliticalLocationByRegionId;
	}

	@Override
	public List<String> getPermissions(Long sysUserId, Long sysPostId) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setSqlGetPoliticalLocation(String sqlGetPoliticalLocation) {
		this.sqlGetPoliticalLocation = sqlGetPoliticalLocation;
	}

	@Override
	public Map<String, Object> getUserByUsername(String userName) {
		return getJdbcTemplate().queryForMap(sqlGetUserByName, userName);
	}
	
	@Override
	public SystemUserDTO getUserBySysUserCode(String sysUserCode) {
		List<SystemUserDTO> res = getJdbcTemplate().query(sqlGetUserByName,
				new BeanPropertyRowMapper<SystemUserDTO>(SystemUserDTO.class),
				sysUserCode);
		if (CollectionUtils.isEmpty(res)) {
			return null;
		}
		return res.get(0);
	}

	@Override
	public void updateLoginNum(Long sysUserId) {
		getJdbcTemplate().update(sqlUpdateLoginNum, sysUserId);
	}

	@Override
	public boolean existRelation(Long sysUserId, Long sysPostId) {
		return getJdbcTemplate().queryForObject(sqlCountPostByUser,
				Integer.class, sysUserId, sysPostId) > 0;
	}

	@Override
	public List<Map<String, Object>> getPostByUId(Long sysUserId) {
		return getJdbcTemplate().queryForList(sqlSelectPostByUser, sysUserId);
	}
	
	@Override
	public List<SystemPostDTO> getPostByUserId(Long sysUserId) {
		return getJdbcTemplate().query(sqlSelectPostByUser,
				new BeanPropertyRowMapper<SystemPostDTO>(SystemPostDTO.class),
				sysUserId);
	}

	@Override
	public List<Map<String, Object>> getRoleByUId(Long sysUserId) {
		return getJdbcTemplate().queryForList(sqlGetRoleIdsByUId, sysUserId);
	}
	
	@Override
	public List<SystemRolesDTO> getRoleByUserId(Long sysUserId) {
		return getJdbcTemplate().query(sqlGetRoleIdsByUId,
				new BeanPropertyRowMapper<SystemRolesDTO>(SystemRolesDTO.class), 
				sysUserId);
	}

	@Override
	public List<Map<String, Object>> selectMenus(Long sysUserId,
			Long sysPostId, String sysCode) {
		return getJdbcTemplate().queryForList(sqlSelectMenu, sysUserId,
				sysPostId, sysUserId, sysPostId, sysCode);
	}
	
	@Override
	public List<FuncMenuDTO> selectMenuList(Long sysUserId, Long sysPostId, String sysCode) {
		return getJdbcTemplate().query(sqlSelectMenu, 
				new BeanPropertyRowMapper<FuncMenuDTO>(FuncMenuDTO.class), 
				sysUserId, sysPostId, sysUserId, sysPostId, sysCode);
	}

	@Override
	public List<Map<String, Object>> selectCompList(Long sysUserId,
			Long sysPostId, String sysCode) {
		return getJdbcTemplate().queryForList(sqlSelectComp, sysCode,
				sysUserId, sysPostId, sysUserId, sysPostId);
	}
	
	@Override
	public List<FuncCompDTO> getCompList(Long sysUserId, Long sysPostId, String sysCode) {
		return getJdbcTemplate().query(sqlSelectComp, 
				new BeanPropertyRowMapper<FuncCompDTO>(FuncCompDTO.class), 
				sysCode, sysUserId, sysPostId, sysUserId, sysPostId);
	}

	@Override
	public Map<String, Object> getOrgById(Long orgId) {
		return getJdbcTemplate().queryForMap(getOrgById, orgId);
	}
	
	@Override
	public OrganizationDTO getOrganizationById(Long orgId) {
		List<OrganizationDTO> res = getJdbcTemplate().query(getOrgById,
				new BeanPropertyRowMapper<OrganizationDTO>(OrganizationDTO.class),
				orgId);
		if (CollectionUtils.isEmpty(res)) {
			return null;
		}
		return res.get(0);
	}


	@Override
	public Map<String, Object> getStaffById(Long staffId) {
		return getJdbcTemplate().queryForMap(getStaffById, staffId);
	}
	
	@Override
	public StaffDTO getStaffByStaffId(Long staffId) {
		List<StaffDTO> res = getJdbcTemplate().query(getStaffById,
				new BeanPropertyRowMapper<StaffDTO>(StaffDTO.class),
				staffId);
		if (CollectionUtils.isEmpty(res)) {
			return null;
		}
		return res.get(0);
	}

	@Override
	public Map<String, Object> getPostById(Long sysPostId) {
		return getJdbcTemplate().queryForMap(getPostById, sysPostId);
	}
	
	@Override
	public SystemPostDTO getPostByPostId(Long sysPostId) {
		List<SystemPostDTO> res = getJdbcTemplate().query(getPostById,
				new BeanPropertyRowMapper<SystemPostDTO>(SystemPostDTO.class),
				sysPostId);
		if (CollectionUtils.isEmpty(res)) {
			return null;
		}
		return res.get(0);
	}

	@Override
	public Map<String, Object> getUserById(Long sysUserId) {
		return getJdbcTemplate().queryForMap(getUserById, sysUserId);
	}
	
	@Override
	public SystemUserDTO getUserByUserId(Long sysUserId) {
		List<SystemUserDTO> res = getJdbcTemplate().query(getUserById,
				new BeanPropertyRowMapper<SystemUserDTO>(SystemUserDTO.class),
				sysUserId);
		if (CollectionUtils.isEmpty(res)) {
			return null;
		}
		return res.get(0);
	}

	@Override
	public void updatePassword(Long sysUserId, String password) {
		getJdbcTemplate().update(updatePassword, password, sysUserId);
	}

	@Override
	public List<Map<String, Object>> getParentRegion(Long commonRegionId) {
		return getJdbcTemplate().queryForList(sqlGetParentRegion,
				commonRegionId);
	}
	
	@Override
	public CommonRegionDTO getParentRegionById(Long commonRegionId) {
		List<CommonRegionDTO> dtoList = getJdbcTemplate().query(sqlGetParentRegion, 
				new BeanPropertyRowMapper<CommonRegionDTO>(CommonRegionDTO.class),
				commonRegionId);
		if(CollectionUtils.isEmpty(dtoList)) return null;
		return dtoList.get(0);
		
	}

	@Override
	public List<Map<String, Object>> getStaffNameById(Long staffId) {
		return getJdbcTemplate().queryForList(sqlGetStaffNameById, staffId);
	}
	
	@Override
	public String getStaffNameByStaffId(Long staffId) {
		List<Map<String, Object>> res = getStaffNameById(staffId);
		if(CollectionUtils.isEmpty(res)) return null;
		Map<String, Object> map = res.get(0);
		return (String)map.get("STAFF_NAME");
	}

	@Override
	public List<Map<String, Object>> getRzOrgIdByStaffId(Long staffId) {
		return getJdbcTemplate().queryForList(sqlGetRzOrgByStaffId, staffId);
	}
	
	@Override
	public Long getRzOrgIdBystaffId(Long staffId) {
		List<Map<String, Object>> res = getRzOrgIdByStaffId(staffId);
		if(CollectionUtils.isEmpty(res)) return null;
		Map<String, Object> map = res.get(0);
		return (Long)map.get("ORG_ID");
	}

	@Override
	public List<Map<String, Object>> getRzOrgCodeById(Long rzOrgId) {
		return getJdbcTemplate().queryForList(sqlGetRzOrgCode, rzOrgId);
	}
	
	@Override
	public String getRzOrgCodeByOrgId(Long rzOrgId) {
		List<Map<String, Object>> res = getRzOrgCodeById(rzOrgId);
		if(CollectionUtils.isEmpty(res)) return null;
		Map<String, Object> map = res.get(0);
		return (String)map.get("ORG_CODE");
	}

	@Override
	public List<Map<String, Object>> getOrgByUserIdAndPostId(Long userId,
			Long postId) {
		return getJdbcTemplate().queryForList(sqlGetOrgByUserIdAndPostId,
				userId, postId);
	}
	
	@Override
	public List<OrganizationDTO> getOrganizationByUserIdAndPostId(Long userId,
			Long postId) {
		return getJdbcTemplate().query(sqlGetOrgByUserIdAndPostId,
				new BeanPropertyRowMapper<OrganizationDTO>(OrganizationDTO.class),
				userId, postId);
	}

	@Override
	public List<Map<String, Object>> getRegionNameById(Long comRegionId) {
		return getJdbcTemplate()
				.queryForList(sqlGetRegionNameById, comRegionId);
	}
	
	@Override
	public String getRegionNameByRegionId(Long comRegionId) {
		List<Map<String, Object>> res = getRegionNameById(comRegionId);
		if(CollectionUtils.isEmpty(res)) return null;
		Map<String, Object> map = res.get(0);
		return (String)map.get("REGION_NAME");
	}

	@Override
	public List<Map<String, Object>> getPoliticalLocation(Long regionId,
			String locType) {
		return getJdbcTemplate().queryForList(sqlGetPoliticalLocation,
				regionId, locType);
	}

	@Override
	public PoliticalLocationDTO getPoliticalLocationByRegionId(Long regionId) {
		List<PoliticalLocationDTO> list = getJdbcTemplate().query(sqlGetPoliticalLocationByRegionId,
				new BeanPropertyRowMapper<PoliticalLocationDTO>(PoliticalLocationDTO.class), 
				regionId);
		if(CollectionUtils.isEmpty(list)) return null;
		return list.get(0);
	}
}
